package classic150;

/**
 * <h3>leetcode</h3>
 *
 * @author BigFaceCat
 * @date 2023/11/24 19:46
 * @description <p>leetCode189-轮转数组-数组-中等题</p>
 */
public class No189 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7};
        int k = 3;
        rotate(nums, k);
        for (int num : nums) {
            System.out.print(num + " ");
        }
    }

    public static void rotate(int[] oldNum, int k) {

        int lens = oldNum.length;
        //取key < nums.length的值，避免多次不必要的转换
        int key = k % lens;
        int[] newNum = new int[lens];

        //第一次把oldNum的前lens - key个数复制给newNum
        System.arraycopy(oldNum, 0, newNum, key, lens - key);
        //第二次把oldNum的后key个数复制给newNum
        System.arraycopy(oldNum, lens - key, newNum, 0, key);
        //第三次把newNum复制给oldNum
        System.arraycopy(newNum, 0, oldNum, 0, lens);
    }
}